<template>
 <lay-layout class="Pop">
    
    <lay-body>
      <lay-table 
            
            :columns="columns"
            :dataSource="dataSource"
            :default-toolbar="defaultToolbar"
            v-model:selectedKeys="selectedKeys"
            even
            @row="rowClick"
         
           
          >
        </lay-table>

    </lay-body>
    <lay-footer>
      <lay-row>
        <lay-col :md=12>
          <lay-select @change="lan_change"
            :options="lan_items" v-model="lan_select"></lay-select>

           

      </lay-col>
        <lay-col :md=12>
          <lay-button @click="model_Copy">复制</lay-button>
        </lay-col>
      </lay-row>
      
     
      <lay-textarea placeholder=""  
      v-model="result"
      :disabled="true"
      style="height: 98%;"
      ></lay-textarea>
 
    </lay-footer>
  </lay-layout>

</template>

<script lang="ts">
import { ref, watch,reactive,onMounted } from 'vue'
import { layer } from '@layui/layer-vue'
import MyHttp from "../../library/MyHttp";  
import MyWin from "../../library/MyWin"; 
import MyPage from "../../library/MyPage"; 
import {MyExcel} from "../../library/MyExcel"; 
import {MySimpleExport,TableColumn} from "../../library/MySimpleExport"; 
import MyData from "../../library/MyData"; 
import MyString from "../../library/MyString"; 
export default {
  props:{
      data: Array 
  },

  setup(prop:any, context:any) {
    const selectedKeys = ref<Array<string>>([]) 
    const selectedObjs=ref<Array<any>>([]);
    const defaultToolbar = ref(false)
    // const page = ref(  new MyPage()  ); 
    const columns = [
      {
        title: '复选',
        width: '50px',
        type: 'checkbox',
        fixed: 'left'
      },
      
      {
        title: '字段名',
        width: '100px' ,
        key: 'ename'
      },
      {
        title: '名称',
        width: '100px', 
        key: 'name' 
      },
      {
        title: '类型',
        width: '100px',
        key: 'lx' 
      } ,
      {
        title: '备注',
        width: '200px',
        key: 'memo' 
      } 
    ]

    
    prop.data.forEach((element: { id: any; ename: any; }) => {
      element.id=element.ename;
    });
    const dataSource = ref(prop.data);
   
    const rowClick = function (data: any) {}

    const rowDoubleClick = function (data: any) {}

    const result=ref<string>("");
     
    const createCol=()=>{
         switch(lan_select.value){
          case "c#":
            createColCharp();
            break;
          case "java":
            createColJava();
            break;
          default:
            createColGo();
            break;
         }
    }
    const createColCharp=()=>{
         
        let s='';
        selectedObjs.value.forEach(element =>{ 
           
          let fieldName=MyString.toUpperCaseCamelCase(element.ename);
          s+=`


[Comment("${element.name}")]
[Schema(Name="${element.name}",Description="${element.memo}")]`;

          if(element.required){
            s+=`
[Required(ErrorMessage = "{0}不能为空")]`;
          }
          
          if(element.vmax!=""){
            s+=`
[MaxLength(${element.vmax})]`;
          }

          
          s+=`
public `;
          switch(element.lx){
            case "Int32":
            case "java.lang.Integer":
              s+=` int `;
              break;
            case "Int64":
            case "java.lang.Long":
              s+=` long `;
              break;
            case "DateTime":
            case "java.util.Date":
              s+=` DateTime `;
              break;
            case "Decimal":
            case "java.math.BigDecimal":
              s+=` Decimal `;
              break;
            case "boolean":
              s+="bool";
              break;
            default:
              if("Gid"==fieldName){
                s+=` Guid `;
              }else{
                s+=` string `;
              }              
              break;
          }
          s+=` ${fieldName} `;
          s+=` {get;set;} `;
          switch(element.lx){
              case "string":
              case "java.lang.String":
                {
                  if("Gid"==fieldName){
                  }else{
                    s+=`="";`;
                  }                  
                }
                break;
          }
        })
        result.value=s; 
    }
    const createColJava=()=>{
        let s='';
        selectedObjs.value.forEach(element =>{ 
          let fieldName=MyString.toLowerCaseCamelCase(element.ename);
          if(element.ename=="id"){
            return 
          }
          if(element.ename=="deleted"){
//             s+=`
// @TableLogic`;
          }
          s+=`
@Schema(name = "${element.name}",description = "${element.memo}")`;
          if(element.required){
            s+=`
@NotBlank(message = "${element.name}不能为空")            `;
          }
          if(element.num){

            if(element.vmax!=""){
            s+=`
@Max(value = ${element.vmax},message ="最大值${element.vmax}")`;
            }

            if(element.vmin!=""){
            s+=`
@Min(value = ${element.vmin},message ="最小值${element.vmin}")`;
            }


          }else{
            if(element.vmax!=""){
            if(element.vmin==""){
              element.vmin="0";
            }
            s+=`
@Size(min =${element.vmin},max = ${element.vmax},message = "长度范围${element.vmin}-${element.vmax}")`;
          }

          }
           
          s+=`
private `;
          switch(element.lx){
            case "Int32":
            case "java.lang.Integer":
              s+=` Integer `;
              break;
            case "Int64":
            case "java.lang.Long":
              s+=` Long `;
              break;
            case "DateTime":
            case "java.util.Date":
              s+=` Date `;
              break;
            case "Decimal":
              s+=` BigDecimal `;
              break;
            case "Boolean":
            case "boolean":
              s+=` boolean`;
              break;
            default:
              s+=` String `;
              break;
          }
          s+=` ${fieldName} `;
          if(element.str){
            s+=`="";`;
          }else if(element.num){
            if(element.lx=="Int64"
            || element.lx== "java.lang.Long"){
              s+=`=0L;`;  
            }else{
              s+=`=0;`;
            }            
          }else{
            s+=`;`;
          }
          s+=`
`;
        })
        result.value=s; 
    }
    const createColGo=()=>{
         
         let s='';
         selectedObjs.value.forEach(element =>{ 
           
           let fieldName=MyString.toUpperCaseCamelCase(element.ename);    
           s+=fieldName.padEnd(15," "); 
           var fieldType="string";
           switch(element.lx){

             case "int":
                fieldType=`int`;
               break;
             case "java.lang.Integer":
             case "Int32":
             case "int32":
               fieldType=`int32`;
               break;
             case "Int64":
             case "int64":
             case "java.lang.Long":
              fieldType=`int64`;
               break;
             case "DateTime":
             case "datetime.MyTime":
             case "java.util.Date":
             fieldType=`datetime.MyTime`;
               break;
             case "Decimal":
             case "java.math.BigDecimal":
             fieldType=`float64`;
               break;
             case "boolean":
             case "bool":
             fieldType="bool";
               break;
             default:
                            
               break;
           }
           if(fieldName.toLocaleLowerCase()=="deleted"){
            fieldType="corestruct.DeletedAt";
           }
           
         

           s+=fieldType.padEnd(8," ");
           
            
           s+='`';
           let jsonFieldName='json:"'+MyString.toLowerCaseCamelCase(element.ename);   
           if(fieldType=="int64"){
            jsonFieldName+=",string"
           }
           jsonFieldName+='"'
           s+=jsonFieldName.padEnd(20," ");

           let validateField="";
           if(element.required){
            validateField+="required,"
           }
           if(element.vmin){
                if(fieldType=="string"){
                  if(element.vmin!="0")
                  {
                    validateField+="min="+element.vmin+",";
                  }                  
                }else{
                  validateField+="gte="+element.vmin+",";
                }
           }
           if(element.vmax){
               
                if(fieldType=="string"){
                  validateField+="max="+element.vmax+",";
                }else{
                  validateField+="lte="+element.vmax+",";
                }
           }
           if(validateField!=""){
            validateField='validate:"'+validateField.substring(0,validateField.length-1)+'"';
            s+=validateField.padEnd(20," ");
           }else{
            s+="".padEnd(20," ");
           }


           let gormFieldName='gorm:"column:'+MyString.toDbCaseCamelCase(element.ename)+';';   
           if(element.vmax){ 
               if(fieldType=="string"){
                gormFieldName+="size:"+element.vmax+";";
               } 
          }
          if(fieldType=="string"){
                gormFieldName+="DEFAULT:'';";
          } else if(fieldType=="int64"){
                gormFieldName+="DEFAULT:0;";
          }  else if(fieldType=="int"){
                gormFieldName+="DEFAULT:0;";
          }   else if(fieldType=="bool"){
                ;
          } else if(fieldType=="corestruct.MyTime"){
               
          } else if(fieldType=="corestruct.DeletedAt"){
            gormFieldName+="DEFAULT:0;type:tinyint(1);";
          }

          if(fieldName.toLocaleLowerCase()=="gid"){
            gormFieldName+="type:char(36);";
           }

          gormFieldName+="not null;";


           gormFieldName+="comment:"+element.name+";";
           if(element.memo){
            gormFieldName+="memo:"+element.memo+";";
           }
           gormFieldName+='"';

            s+=gormFieldName;
           s+='`';
           s+="\r\n";
         })
         result.value=s; 
     }
    watch(
      selectedKeys,(newVal:Array<string>,oldVal:Array<string>)=>{
         
        let addVal=MyData.keyAdd(oldVal,newVal)  ;
        let removeVal=MyData.keyRemove(oldVal,newVal)  ;
         
        removeVal.forEach(element=>{
            MyData.removeDataById(selectedObjs.value,element);          
        })

        addVal.forEach(element=>{
            let m=MyData.getById(selectedObjs.value,element);
            if(!m){
              m=MyData.getById(dataSource.value,element);
              selectedObjs.value.push(m)
            }             
        })
        createCol();
      }
    )
    onMounted(() =>{  
     
    });
    const IsJava=ref<boolean>(false);

    const lan_select=ref<any>("java");
    const lan_items=[
    {label:'java', value:"java"},
    {label:'c#', value:"c#"},
    {label:'go', value:"go"},
    ]
    const lan_change=(row:any)=>{
      createCol();
    }
    const model_Copy=(row:any)=>{
      let s='';
        selectedObjs.value.forEach(element =>{ 
          
          let fieldName=MyString.toUpperCaseCamelCase(element.ename);    

          s+=`m.${fieldName}=mm.${fieldName}\r\n`;

        });
        result.value=s; 
    }
    return {
      columns,
      dataSource,
      selectedKeys, 
      defaultToolbar,
      //page,
      rowClick,
      rowDoubleClick,
      
      selectedObjs,
      createCol,
      result,

      IsJava,

      lan_select,
      lan_items,
      lan_change,

      model_Copy
    }
  }
}
</script>
<style scoped>
.Pop .layui-footer,
.Pop .layui-header {
  height: 200px;
  text-align: center; 
   
}  
</style>